﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows;

namespace CreateUserSpecificSettings
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void buttonEncryptToRegistry_Click(object sender, RoutedEventArgs e)
        {
            string dataToEncrypt =
                tbSqlDatabaseEndpoint.Text + "\n" +
                tbSqlDatabaseInstanceName.Text + "\n" +
                tbSqlDatabaseUsername.Text + "\n" +
                tbSqlDatabasePassword.Text + "\n" +
                tbSqlDatabaseCatalogName.Text + "\n" +
                tbPrintServerEndpoint.Text + "\n" +
                tbPrintServerUsername.Text + "\n" +
                tbPrintServerPassword.Text + "\n" +
                tbSystemPassword.Text + "\n" +
                tbIsCustomLook.Text + "\n";

            Encoding asciiEncover = Encoding.ASCII;
            string textPassword = tbPassword.Text;
            byte[] plainPassword = Convert.FromBase64String(textPassword);
            byte[] initializationVector = asciiEncover.GetBytes("skcju902mnhjsnwh");
            byte[] encrypted = encryptStringToBytes_AES(dataToEncrypt, plainPassword, initializationVector);

            using (FileStream fs = new FileStream("oceanside.sys", FileMode.Create))
            {
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    bw.Write(encrypted);
                }
            }
        }

        static byte[] encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (plainText == null || plainText.Length <= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");

            // Declare the stream used to encrypt to an in memory
            // array of bytes.
            MemoryStream msEncrypt = null;

            // Declare the RijndaelManaged object
            // used to encrypt the data.
            RijndaelManaged aesAlg = null;

            try
            {
                // Create a RijndaelManaged object
                // with the specified key and IV.
                aesAlg = new RijndaelManaged();
                aesAlg.Key = Key;
                aesAlg.IV = IV;

                // Create an encryptor to perform the stream transform.
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for encryption.
                msEncrypt = new MemoryStream();
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream.
                        swEncrypt.Write(plainText);
                    }
                }
            }
            finally
            {
                // Clear the RijndaelManaged object.
                if (aesAlg != null)
                    aesAlg.Clear();
            }

            // Return the encrypted bytes from the memory stream.
            return msEncrypt.ToArray();
        }

        static string decryptStringFromBytes_AES(byte[] cipherText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");

            // Declare the RijndaelManaged object
            // used to decrypt the data.
            RijndaelManaged aesAlg = null;

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            try
            {
                // Create a RijndaelManaged object
                // with the specified key and IV.
                aesAlg = new RijndaelManaged();
                aesAlg.Key = Key;
                aesAlg.IV = IV;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))

                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }
            finally
            {
                // Clear the RijndaelManaged object.
                if (aesAlg != null)
                    aesAlg.Clear();
            }

            return plaintext;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonDecrypt_Click(object sender, RoutedEventArgs e)
        {
            tbSanityCheck.Text = "";
            using (FileStream fs = new FileStream("oceanside.sys", FileMode.Open))
            {
                Encoding asciiEncover = Encoding.ASCII;
                byte[] readBytes = new byte[2048];
                int bytesRead = fs.Read(readBytes, 0, 2048);
                byte[] deycryptedBytes = new byte[bytesRead];

                for (int i = 0; i < bytesRead; i++)
                {
                    deycryptedBytes[i] = readBytes[i];
                }

                byte[] plainPassword = Convert.FromBase64String(tbPassword.Text);
                byte[] initializationVector = asciiEncover.GetBytes("skcju902mnhjsnwh");

                string decryptString = decryptStringFromBytes_AES(deycryptedBytes, plainPassword, initializationVector);
                tbSanityCheck.Text = decryptString;
            }
        }
    }
}
